<?php
if($_SESSION['memberID']!=1 && !($_SESSION['sid']==1 && strpos($_SESSION['qx'],'ali_refund'))){
    exit("不合法的身份访问！");
}
include '../common/autoload.php';
include("/var/www/html/new/d/common/page.php");

include("/var/www/html/alipay/account.config.php");
include_once '/var/www/html/new/com.inc.php';

$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s= new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

//去除票付通的支付宝的ID
//print_r($account_list);
//print_r($account_list);
//获取类似鼓浪屿/云顶这种只用自己的支付宝账户的供应商
$customizing_ali_account = array_keys($account_list);
array_shift($customizing_ali_account);

//print_r($customizing_ali_account);
//$customizing_alipay_account =
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:20;
$offset=($currentPage-1)*$pageSize;
$aid_black_list = array(7517, 7132, 7133, 7134, 7135, 7136, 24863,);
$where = "refund_money>0 AND aid NOT IN(".implode(',', $aid_black_list).")";
if ($_REQUEST['ordernum']) {
    $where .= " AND ordernum='".trim($_REQUEST['ordernum'])."'";
}
if (isset($_GET['payMode']) && $_GET['payMode']!=-1) {
    $where .= " AND sourceT=" . intval($_GET['payMode']);
}
if (isset($_GET['refund_status']) && $_GET['refund_status']!=-1) {
    $where .= " AND refund_status=" . intval($_GET['refund_status']);
}
$sqlTotal = "SELECT COUNT(*) AS cnt FROM pft_order_refund WHERE $where";
$GLOBALS['le']->query($sqlTotal);
$GLOBALS['le']->fetch_assoc();
$total = $GLOBALS['le']->f('cnt');

$sql = <<<SQL
SELECT id, aid,ordernum, refund_num, refund_money, refund_fee,
refund_time, handler_time, refund_status, batch_no, trade_no, subject,sourceT
FROM pft_order_refund WHERE $where ORDER BY refund_time DESC,refund_status ASC LIMIT $offset,$pageSize
SQL;
$GLOBALS['le']->query($sql);
while($tmp =$GLOBALS['le']->fetch_assoc()) {
    $r[] = $tmp;
}
//foreach($r as $key=>$val){
//    $order=$val['ordernum'];
//    $info=$s->Order_Globle_Search('', '', '', '', '', '', '', '', '', '', '',
//        '', $order, '', '', '', '', '', '', '1', '0', '1', '0', '0', '',
//        '0', '', '0', '', '', '', '0', '0', '0');
//    $obj=simplexml_load_string($info);
//    $status=$obj->Rec->UUstatus;
//    if($status==0){
//        $r[$key]['order_status']='未使用';
//    }elseif($status==1){
//       $r[$key]['order_status']='已使用';
//    }elseif($status==2){
//       $r[$key]['order_status']='已过期';
//    }elseif($status==3){
//       $r[$key]['order_status']='被取消';
//    }elseif($status==4){
//       $r[$key]['order_status']='凭证码被替代';
//    }elseif($status==5){
//       $r[$key]['order_status']='被终端修改';
//    }elseif($status==6){
//       $r[$key]['order_status']='被终端撤销';
//    }elseif($status==7){
//       $r[$key]['order_status']='部分使用';
//    }
//}

$dstatusT = array(
    '<span style="color:#F00;">未处理</span>',
    '<span style="color:#2d1eff;">已处理</span>',
);
$dnames = include '../module/link_product/Member.cache.php';
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>支付宝退款申请</title>
    <link rel="stylesheet" href="/css/aliceui.min.css"/>
    <link rel="stylesheet" href="/css/resetui.css"/>
    <link rel="stylesheet" href="/css/page.css"/>
    <link rel="stylesheet" href="css/list.css"/>
</head>
<body>
<div class="wrap">
    <form name="ourform" class="search_form ">
        <input name="gid" type="hidden" value="<?=$_GET['gid']?>"/>
        <fieldset class="search-box">
            <legend>退款记录查询</legend>
            <label>订单号：</label><input type="text" name="ordernum" value="<?=$ordernum?>" class="ui-input"/>
            <label for="payMode">支付方式：</label>
            <select name="payMode" id="payMode">
                <option value="-1">--不限--</option>
                <option value="0" <?=isset($_GET['payMode']) && $_GET['payMode']==0 ? 'selected':''?>>支付宝支付</option>
                <option value="1" <?=$_GET['payMode']==1 ? 'selected':''?>>微信支付</option>
                <option value="2" <?=$_GET['payMode']==2 ? 'selected':''?>>银联支付</option>
            </select>
            <label for="payMode">退款状态：</label>
            <select name="refund_status" id="refund_status">
                <option value="-1">--不限--</option>
                <option value="0" <?=isset($_GET['refund_status']) && $_GET['refund_status']==0 ? 'selected':''?>>未退款</option>
                <option value="1" <?=$_GET['refund_status']==1 ? 'selected':''?>>已退款</option>
            </select>
            <input type="submit" value="查 询" class="ui-button ui-button-lblue"/>
            <p>
                说明：如支付宝退款失败并提示“同一批退款中存在两条相同的退款记录”，请检查勾选的订单中是否有重复的订单号；如果有重复的订单号，需要一笔一笔退款。
                <br/>
                <span style="color: red;">2015年3月10日前通过手机端支付宝支付的订单无法使用退款接口；需辛苦财务手动退款到客户的支付宝账户。</span>
            </p>
        </fieldset>
    </form>
    <div class="ui-table-container">
        <table id="listTable" class="ui-table ui-table-noborder"><!-- 可以在class中加入ui-table-inbox或ui-table-noborder分别适应不同的情况 -->
            <thead>
            <tr>
                <th>编号</th>
                <th class="fir">订单号</th>
                <th>退款金额</th>
                <th>手续费</th>
                <th>原因</th>
                <th>供应商</th>
                <th>申请时间</th>
                <th>操作时间</th>
                <th>支付宝/微信/银联支付</th>
                <th>退款状态</th>
                <th>订单状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <form id="pay_frm" action="http://pay.12301.cc/refund_api.php" target="_blank" method="post">
                <input name="from" type="hidden" value="<?=$_SESSION['memberID']?>"/>
                <?php if(count($r)):?>
                    <?foreach($r as $row):?>
                        <tr>
                            <td class="chk">
                                <input class="tids"
                                       type="checkbox"
                                    <?=$row['sourceT']==1 || $row['refund_status']==1 ? 'disabled' :''?>
                                       name="log_id[]"
                                       value="<?=$row['id']?>"/>
                                <?=$row['id']?>
                            </td>
                            <td title="<?=$row['fxid']?>">
                                <a target="_blank" href="/orderView.html?ordernum=<?=$row['ordernum']?>"><?=$row['ordernum']?></a>
                            </td>
                            <td><?=$row['refund_money']/100?></td>
                            <td><?=$row['refund_fee']/100?></td>
                            <td><?=$row['subject']?></td>
                            <td><?=$dnames[$row['aid']]?></td>
                            <td><?=$row['refund_time']?></td>
                            <td><?=$row['handler_time']?></td>
                            <td><?
                                switch($row['sourceT']){
                                    case 0:
                                        echo '<img style="height: 24px;" src="http://wx.12301.cc/public/images/icon_alipay.gif" alt="支付宝"/>';
                                        break;
                                    case 1:
                                        echo '<img style="height: 24px;" src="http://wx.12301.cc/public/images/icon_wx_logo.png" alt="微信支付"/>';
                                        break;
                                    case 2:
                                        echo '<img style="height: 24px;" src="http://wx.12301.cc/public/images/icon_uni_logo.gif" alt="银联支付"/>';
                                        break;
                                }                '. '
                                ?></td>
                            <td id="log_status_<?=$row['id']?>"><?=$dstatusT[$row['refund_status']]?></td>
                            <td><a href="javascript:;" class="show-order-status" data-ordernum="<?=$row['ordernum']?>">查看</a> </td>
                            <td>
                                <?php if($row['refund_status']==0 && $row['sourceT']==0){?>
                                    <a class="oper" href="javascript:;" data-ordernum="<?=$row['ordernum']?>">退款</a>
                                    <br/>支付宝没有退款？点这：
                                    <a style="color:red;" class="refundManual" href="javascript:;"
                                       data-id="<?=$row['id']?>"
                                       data-manual="1"
                                       data-ordernum="<?=$row['ordernum']?>" data-msg="这个操作很危险，未了解清楚不要点啊！">手工退款</a>

                                <?php }elseif($row['refund_status']==0 && $row['sourceT']==1){?>
                                    <a class="refundManual" href="javascript:;"
                                       data-id="<?=$row['id']?>"
                                       data-manual="0"
                                       data-ordernum="<?=$row['ordernum']?>">退款</a>
                                    <br/>微信没有退款？点这：
                                    <a style="color:red;" data-manual="1" class="refundManual" href="javascript:;"
                                       data-id="<?=$row['id']?>"
                                       data-ordernum="<?=$row['ordernum']?>" data-msg="这个操作很危险，未了解清楚不要点啊！">手工退款</a>
                                <?php }elseif($row['refund_status']==0 && $row['sourceT']==2){?>
                                    <a class="uni_refund" href="javascript:;"
                                       data-id="<?=$row['id']?>"
                                       data-ordernum="<?=$row['ordernum']?>">退款</a>
                                <?php }else{?>
                                    已处理
                                <?php }?>
                            </td>
                        </tr>
                    <?endforeach;?>
                    </tbody>
                    <tfoot>
                    <tr>
                        <td>
                            <a id="selectAllBtn" class="ckall" href="javascript:;">全选</a>
                            <a id="cannelAllBtn" class="reck" href="javascript:;">取消已选中的</a>
                        </td>
                        <td colspan="9" style="text-align: center;">
                            <input type="submit" class="ui-button ui-button-lorange" value="批量退款"/>
                        </td>
                    </tr>
                    </tfoot>
                <?php endif;?>
            </form>
        </table>
        <?page($total,$currentPage,$pageSize);?>
    </div>
</div>
<script src="../js/jquery-1.9.1.min.js"></script>
<script>
    var selectAll = function(){
        $("#listTable").find(".tids").each(function(index,inp){
            $(inp).prop("checked","checked");
        })
    };
    var cancelAll = function(){
        $("#listTable").find(".tids").each(function(index,inp){
            $(inp).prop("checked","");
        })
    };
    $(document).ready(function(){
        $(".ui-table").on('click', 'a.oper',function(e){
            cancelAll();
            $(e.currentTarget).parents("tr").find('input.tids').prop('checked',"checked");
            $("#pay_frm").submit();
        });
        //全选
        $("#selectAllBtn").on("click",function(e){
            selectAll();
        })
        //全不选
        $("#cannelAllBtn").on("click",function(e){
            cancelAll();
        })
        $(".ui-table").on('click', 'a.refundManual', function(){
            var obj = $(this);
            if (obj.hasClass('ing')) {
                alert('处理中，别急...');
                return false;
            } else {
                obj.addClass('ing');
            }
            var alert_msg = '您确定要执行退款操作吗？';
//                        +'\n微信退款接口出现故障，票付通平台还未对接上；'
//                        +'\n退款需要登陆到微信支付平台手动操作,执行此操作前请确保您已在微信后台提交退款申请。';
            if ($(this).data('msg')) {
                alert_msg = $(this).data('msg');
                if (!confirm(alert_msg)) {
                    obj.removeClass('ing')
                    return false;
                }
                if (!confirm('真的了解清楚了吗？')) {
                    return false;
                }
            }
            else if (!confirm(alert_msg)) {
                obj.removeClass('ing')
                return false;
            }
            var param = {
                action:'refundManual',
                log_id : $(this).data('id'),
                order_num:$(this).data('ordernum'),
                manual : $(this).data('manual')
            };
            $.post('wx_refund.php',param, function(json){
                if (json.code=='200') {
                    obj.parent().text('');
                    $("#log_status_"+param['log_id']).html('<span style="color:#2d1eff;">已处理</span>');
                } else {
                    alert(json.msg);
                }
            },'json');
        });
        $(".ui-table").on('click', 'a.uni_refund', function(){
            var obj = $(this);
            if (obj.hasClass('ing')) {
                alert('处理中，别急...');
                return false;
            } else {
                obj.addClass('ing');
            }
            var alert_msg = '您确定要执行退款操作吗？';
            if(!confirm(alert_msg)){
                obj.removeClass('ing')
                return false;
            }
            var param = {
                log_id : $(this).data('id'),
                order_num:$(this).data('ordernum'),
            };
            $.post('//www.12301.cc/module/zax/unipay/Form_6_4_Refund.php',param, function(json){
                if (json.code=='200') {
                    obj.parent().text('');
                    $("#log_status_"+param['log_id']).html('<span style="color:#2d1eff;">已处理</span>');
                } else {
                    alert(json.msg);
                }
            },'json');
        });
        $(".ui-table").on("click",'a.show-order-status',function(){
            var order_num = $(this).attr('data-ordernum'),
                change = $(this);
            var param = {
                action:'order_status',
                ordernum: order_num
            };
            $.post('jh.php',param,function(json){
                var sp="<span style='color:purple'>"+json.status+"</span>";
                change.parent().html(sp);
            },'json');
        });
    });
</script>
</body>
</html>